#ifndef B4E5BD62_8B1A_4A3C_A419_5662CF28CAD4
#define B4E5BD62_8B1A_4A3C_A419_5662CF28CAD4


#endif /* B4E5BD62_8B1A_4A3C_A419_5662CF28CAD4 */

#include <iostream>
#include <string>
#include "Log.hpp"
// #include "Init.hpp"

extern Log lg;

class Task
{
public:
    Task(int sockfd, const string& clientip, const uint16_t& clientport)
        :sockfd_(sockfd), clientip_(clientip),clientport_(clientport)
    {}
    Task()
    {}

    void Run()
    {
        // 测试
        char buffer[4096];
        // Tcp是面向字节流的，如何保证读取上来的数据是一个完整的报文嘞
        ssize_t n = read(sockfd_, buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            cout << "client say# " << buffer << endl;
            string echo_string = buffer;
            
            // 开始写入网络
            n = write(sockfd_, echo_string.c_str(), echo_string.size());
            if(n < 0)
            {
                lg(Warning, "write error, errno: %d, errstring: %s", errno, strerror(errno));
            }
        }
        else if(n == 0)
        {
            lg(Info, "%s:%d, server close sockfd: %d", clientip_.c_str(), clientport_, sockfd_);
        }
        else
        {
            lg(Warning, "read error, sockfd: %d, client ip: %s, client port: %d",  sockfd_, clientip_.c_str(), clientport_);
        }
        close(sockfd_);
    }

    void operator() ()
    {
        Run();
    }

    ~Task()
    {}
private:
    int sockfd_;
    string clientip_;
    uint16_t clientport_;
};

